home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 4X2PZ (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  7.9 KB  |  387 lines

  1. package com.sun.java.swing.plaf.basic;
  2.  
  3. import com.sun.java.swing.tree.DefaultMutableTreeNode;
  4. import com.sun.java.swing.tree.TreeModel;
  5. import com.sun.java.swing.tree.TreePath;
  6. import com.sun.java.swing.tree.TreeSelectionModel;
  7.  
  8. public class LargeTreeModelNode extends DefaultMutableTreeNode {
  9.    protected AbstractTreeUI treeUI;
  10.    protected boolean isExpanded;
  11.    protected int childIndex;
  12.  
  13.    public LargeTreeModelNode(AbstractTreeUI treeUI, Object userObject, int childIndex) {
  14.       super(userObject);
  15.       this.treeUI = treeUI;
  16.       this.childIndex = childIndex;
  17.    }
  18.  
  19.    protected void addLargeTreeModelNode(LargeTreeModelNode newChild) {
  20.       boolean added = false;
  21.       int childIndex = newChild.getChildIndex();
  22.       int counter = 0;
  23.  
  24.       for(int maxCounter = ((DefaultMutableTreeNode)this).getChildCount(); counter < maxCounter; ++counter) {
  25.          if (((LargeTreeModelNode)((DefaultMutableTreeNode)this).getChildAt(counter)).getChildIndex() > childIndex) {
  26.             added = true;
  27.             ((DefaultMutableTreeNode)this).insert(newChild, counter);
  28.             counter = maxCounter;
  29.          }
  30.       }
  31.  
  32.       if (!added) {
  33.          ((DefaultMutableTreeNode)this).add(newChild);
  34.       }
  35.  
  36.    }
  37.  
  38.    protected void adjustLargeRowCountBy(int changeAmount) {
  39.       AbstractTreeUI var10000 = this.treeUI;
  40.       var10000.largeRowCount += changeAmount;
  41.    }
  42.  
  43.    public LargeTreeModelNode childAtModelIndex(int index) {
  44.       for(int counter = ((DefaultMutableTreeNode)this).getChildCount() - 1; counter >= 0; --counter) {
  45.          if (((LargeTreeModelNode)((DefaultMutableTreeNode)this).getChildAt(counter)).childIndex == index) {
  46.             return (LargeTreeModelNode)((DefaultMutableTreeNode)this).getChildAt(counter);
  47.          }
  48.       }
  49.  
  50.       return null;
  51.    }
  52.  
  53.    protected void childInsertedAtModelIndex(int index) {
  54.       int counter = 0;
  55.  
  56.       for(int maxCounter = ((DefaultMutableTreeNode)this).getChildCount(); counter < maxCounter; ++counter) {
  57.          LargeTreeModelNode aChild = (LargeTreeModelNode)((DefaultMutableTreeNode)this).getChildAt(counter);
  58.          if (aChild.childIndex >= index) {
  59.             while(counter < maxCounter) {
  60.                ++((LargeTreeModelNode)((DefaultMutableTreeNode)this).getChildAt(counter)).childIndex;
  61.                ++counter;
  62.             }
  63.          }
  64.       }
  65.  
  66.    }
  67.  
  68.    protected void childRemovedAtModelIndex(int index) {
  69.       int counter = 0;
  70.  
  71.       for(int maxCounter = ((DefaultMutableTreeNode)this).getChildCount(); counter < maxCounter; ++counter) {
  72.          LargeTreeModelNode aChild = (LargeTreeModelNode)((DefaultMutableTreeNode)this).getChildAt(counter);
  73.          if (aChild.childIndex >= index) {
  74.             while(counter < maxCounter) {
  75.                --((LargeTreeModelNode)((DefaultMutableTreeNode)this).getChildAt(counter)).childIndex;
  76.                ++counter;
  77.             }
  78.          }
  79.       }
  80.  
  81.    }
  82.  
  83.    public void collapse(boolean adjustTree) {
  84.       if (this.isExpanded) {
  85.          TreeSelectionModel selModel = null;
  86.          TreePath[] selPaths;
  87.          if (this.treeUI != null && (selModel = this.treeUI.getSelectionModel()) != null) {
  88.             selPaths = selModel.getSelectionPaths();
  89.          } else {
  90.             selPaths = null;
  91.          }
  92.  
  93.          if (this.isVisible()) {
  94.             this.adjustLargeRowCountBy(-this.getTotalChildCount());
  95.          }
  96.  
  97.          this.isExpanded = false;
  98.          if (adjustTree) {
  99.             this.didAdjustTree();
  100.             this.treeUI.visibleNodesChanged();
  101.          }
  102.  
  103.          this.treeUI.pathWasCollapsed(new TreePath(((DefaultMutableTreeNode)this).getUserObjectPath()));
  104.          if (selPaths != null) {
  105.             boolean shouldRemove = false;
  106.             TreePath ourPath = new TreePath(((DefaultMutableTreeNode)this).getUserObjectPath());
  107.  
  108.             for(int counter = selPaths.length - 1; counter >= 0; --counter) {
  109.                if (selPaths[counter] != null && ourPath.isDescendant(selPaths[counter]) && !ourPath.equals(selPaths[counter])) {
  110.                   shouldRemove = true;
  111.                } else {
  112.                   selPaths[counter] = null;
  113.                }
  114.             }
  115.  
  116.             if (shouldRemove) {
  117.                selModel.removeSelectionPaths(selPaths);
  118.             }
  119.          }
  120.       }
  121.  
  122.    }
  123.  
  124.    protected void didAdjustTree() {
  125.    }
  126.  
  127.    public void expand(boolean adjustTree) {
  128.       if (!this.isExpanded) {
  129.          boolean visible = this.isVisible();
  130.          this.isExpanded = true;
  131.          if (visible) {
  132.             this.adjustLargeRowCountBy(this.getTotalChildCount());
  133.          }
  134.  
  135.          if (adjustTree) {
  136.             this.didAdjustTree();
  137.             this.treeUI.visibleNodesChanged();
  138.          }
  139.  
  140.          this.treeUI.pathWasExpanded(new TreePath(((DefaultMutableTreeNode)this).getUserObjectPath()));
  141.          if (this.treeUI != null && visible) {
  142.             int row = this.getRow();
  143.             TreeSelectionModel selModel = this.treeUI.getSelectionModel();
  144.             int cCount;
  145.             if (selModel != null && (cCount = this.getTotalChildCount()) > 0 && selModel.isRowSelected(row) && selModel.isRowSelected(row + 1)) {
  146.                TreePath[] paths = new TreePath[cCount];
  147.  
  148.                for(int counter = 0; counter < cCount; ++counter) {
  149.                   paths[counter] = this.treeUI.getPathForRow(counter + row + 1);
  150.                }
  151.  
  152.                selModel.addSelectionPaths(paths);
  153.             } else if (selModel != null) {
  154.                selModel.resetRowSelection();
  155.             }
  156.          }
  157.       }
  158.  
  159.    }
  160.  
  161.    public int getChildIndex() {
  162.       return this.childIndex;
  163.    }
  164.  
  165.    protected int getCountTo(int stopIndex) {
  166.       int retCount = stopIndex + 1;
  167.       int counter = 0;
  168.  
  169.       for(int maxCounter = ((DefaultMutableTreeNode)this).getChildCount(); counter < maxCounter; ++counter) {
  170.          LargeTreeModelNode aChild = (LargeTreeModelNode)((DefaultMutableTreeNode)this).getChildAt(counter);
  171.          if (aChild.childIndex >= stopIndex) {
  172.             counter = maxCounter;
  173.          } else {
  174.             retCount += aChild.getTotalChildCount();
  175.          }
  176.       }
  177.  
  178.       if (super.parent != null) {
  179.          return retCount + ((LargeTreeModelNode)((DefaultMutableTreeNode)this).getParent()).getCountTo(this.childIndex);
  180.       } else if (!this.treeUI.isRootVisible()) {
  181.          return retCount - 1;
  182.       } else {
  183.          return retCount;
  184.       }
  185.    }
  186.  
  187.    public TreeModel getModel() {
  188.       return this.treeUI.getModel();
  189.    }
  190.  
  191.    protected boolean getPathForRow(int row, int[] rowCounter, TreePath[] retPath, LargeTreeModelNode[] eNode, boolean[] isParentNode, int[] childIndex) {
  192.       if (row == rowCounter[0]) {
  193.          if (childIndex != null) {
  194.             childIndex[0] = this.getChildIndex();
  195.          }
  196.  
  197.          if (eNode != null) {
  198.             eNode[0] = this;
  199.             isParentNode[0] = false;
  200.          }
  201.  
  202.          if (retPath != null) {
  203.             retPath[0] = new TreePath(((DefaultMutableTreeNode)this).getUserObjectPath());
  204.          }
  205.  
  206.          return true;
  207.       } else {
  208.          int var10002 = rowCounter[0]++;
  209.          if (this.isExpanded) {
  210.             int lastChildIndex = 0;
  211.             TreeModel treeModel = this.getModel();
  212.             int counter = 0;
  213.  
  214.             for(int maxCounter = ((DefaultMutableTreeNode)this).getChildCount(); counter < maxCounter; ++counter) {
  215.                LargeTreeModelNode aNode = (LargeTreeModelNode)((DefaultMutableTreeNode)this).getChildAt(counter);
  216.                int newChildIndex = aNode.childIndex;
  217.                if (rowCounter[0] + (newChildIndex - lastChildIndex) > row) {
  218.                   if (childIndex != null) {
  219.                      childIndex[0] = row - rowCounter[0] + lastChildIndex;
  220.                   }
  221.  
  222.                   if (retPath != null) {
  223.                      Object[] thisPath = ((DefaultMutableTreeNode)this).getUserObjectPath();
  224.                      int pLength = thisPath.length;
  225.                      Object[] newPath = new Object[pLength + 1];
  226.                      Object child = treeModel.getChild(super.userObject, row - rowCounter[0] + lastChildIndex);
  227.                      System.arraycopy(thisPath, 0, newPath, 0, pLength);
  228.                      newPath[pLength] = child;
  229.                      retPath[0] = new TreePath(newPath);
  230.                   }
  231.  
  232.                   if (eNode != null) {
  233.                      eNode[0] = this;
  234.                      isParentNode[0] = true;
  235.                   }
  236.  
  237.                   return true;
  238.                }
  239.  
  240.                rowCounter[0] += newChildIndex - lastChildIndex;
  241.                lastChildIndex = newChildIndex + 1;
  242.                if (aNode.getPathForRow(row, rowCounter, retPath, eNode, isParentNode, childIndex)) {
  243.                   return true;
  244.                }
  245.             }
  246.  
  247.             int var17 = treeModel.getChildCount(super.userObject) - 1;
  248.             if (var17 - lastChildIndex >= 0) {
  249.                if (rowCounter[0] + (var17 - lastChildIndex) >= row) {
  250.                   if (childIndex != null) {
  251.                      childIndex[0] = row - rowCounter[0] + lastChildIndex;
  252.                   }
  253.  
  254.                   if (retPath != null) {
  255.                      Object[] thisPath = ((DefaultMutableTreeNode)this).getUserObjectPath();
  256.                      int pLength = thisPath.length;
  257.                      Object[] newPath = new Object[pLength + 1];
  258.                      Object var18 = treeModel.getChild(super.userObject, row - rowCounter[0] + lastChildIndex);
  259.                      System.arraycopy(thisPath, 0, newPath, 0, pLength);
  260.                      newPath[pLength] = var18;
  261.                      retPath[0] = new TreePath(newPath);
  262.                   }
  263.  
  264.                   if (eNode != null) {
  265.                      eNode[0] = this;
  266.                      isParentNode[0] = true;
  267.                   }
  268.  
  269.                   return true;
  270.                }
  271.  
  272.                rowCounter[0] += var17 - lastChildIndex + 1;
  273.             }
  274.          }
  275.  
  276.          return false;
  277.       }
  278.    }
  279.  
  280.    public int getRow() {
  281.       LargeTreeModelNode parent = (LargeTreeModelNode)((DefaultMutableTreeNode)this).getParent();
  282.       if (parent == null) {
  283.          return this.treeUI.isRootVisible() ? 0 : -1;
  284.       } else {
  285.          return parent.getCountTo(this.childIndex);
  286.       }
  287.    }
  288.  
  289.    protected boolean getRow(Object[] path, int pathCounter, int pathLength, boolean isInPath, int[] rowCounter) {
  290.       isInPath = isInPath && path[pathCounter].equals(super.userObject);
  291.       if (isInPath) {
  292.          ++pathCounter;
  293.          if (pathCounter == pathLength) {
  294.             return true;
  295.          }
  296.       }
  297.  
  298.       int var10002 = rowCounter[0]++;
  299.       if (this.isExpanded) {
  300.          TreeModel treeModel = this.getModel();
  301.          int endIndex;
  302.          if (isInPath && pathCounter + 1 == pathLength) {
  303.             endIndex = treeModel.getIndexOfChild(super.userObject, path[pathLength - 1]);
  304.          } else {
  305.             endIndex = Integer.MAX_VALUE;
  306.          }
  307.  
  308.          int lastChildIndex = 0;
  309.          int newRowCount = rowCounter[0];
  310.          int counter = 0;
  311.  
  312.          for(int maxCounter = ((DefaultMutableTreeNode)this).getChildCount(); counter < maxCounter; ++counter) {
  313.             LargeTreeModelNode aNode = (LargeTreeModelNode)((DefaultMutableTreeNode)this).getChildAt(counter);
  314.             int newChildIndex = aNode.childIndex;
  315.             if (newChildIndex >= endIndex) {
  316.                rowCounter[0] = newRowCount + (endIndex - lastChildIndex);
  317.                return true;
  318.             }
  319.  
  320.             newRowCount += newChildIndex - lastChildIndex;
  321.             lastChildIndex = newChildIndex + 1;
  322.             rowCounter[0] = newRowCount;
  323.             if (aNode.getRow(path, pathCounter, pathLength, isInPath, rowCounter)) {
  324.                return true;
  325.             }
  326.  
  327.             newRowCount = rowCounter[0];
  328.          }
  329.  
  330.          int var15 = treeModel.getChildCount(super.userObject) - 1;
  331.          if (var15 >= 0) {
  332.             if (var15 >= endIndex) {
  333.                rowCounter[0] = newRowCount + (endIndex - lastChildIndex);
  334.                return true;
  335.             }
  336.  
  337.             rowCounter[0] += var15 - lastChildIndex + 1;
  338.          }
  339.       }
  340.  
  341.       return false;
  342.    }
  343.  
  344.    public int getTotalChildCount() {
  345.       if (!this.isExpanded()) {
  346.          return 0;
  347.       } else {
  348.          int retCount = this.getModel().getChildCount(((DefaultMutableTreeNode)this).getUserObject());
  349.  
  350.          for(int counter = ((DefaultMutableTreeNode)this).getChildCount() - 1; counter >= 0; --counter) {
  351.             retCount += ((LargeTreeModelNode)((DefaultMutableTreeNode)this).getChildAt(counter)).getTotalChildCount();
  352.          }
  353.  
  354.          return retCount;
  355.       }
  356.    }
  357.  
  358.    public int getVisibleLevel() {
  359.       return this.treeUI.isRootVisible() ? ((DefaultMutableTreeNode)this).getLevel() : ((DefaultMutableTreeNode)this).getLevel() - 1;
  360.    }
  361.  
  362.    public boolean isExpanded() {
  363.       return this.isExpanded;
  364.    }
  365.  
  366.    public boolean isVisible() {
  367.       LargeTreeModelNode parent = (LargeTreeModelNode)((DefaultMutableTreeNode)this).getParent();
  368.       if (parent == null) {
  369.          return true;
  370.       } else {
  371.          return parent.isExpanded() && parent.isVisible();
  372.       }
  373.    }
  374.  
  375.    public void modelChildCountChanged() {
  376.    }
  377.  
  378.    public void toggleExpanded() {
  379.       if (this.isExpanded()) {
  380.          this.collapse(true);
  381.       } else {
  382.          this.expand(true);
  383.       }
  384.  
  385.    }
  386. }
  387.